<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数组类型</title>
    <script>
        function f1() {
            // 定义一个数组类型的变量
            // 此时数组长度为0,没有元素
            // let arr = new Array();
            // 也可以指定长度
            let arr = new Array(5);
            console.log("数组长度:" + arr.length)
            arr[0] = 1;
            arr[1] = 2;
            arr[5] = 5;
            console.log("数组长度:" + arr.length)
            // 数组没有默认值,如果指定下标的元素没有赋值,相当于声明了变量但是没有赋值
            for (let i = 0; i < arr.length; i++) {
                console.log(arr[i])
            }
        }

        function f2() {
            // 指定数组中初始元素
            let arr = new Array(1, 2, 3, 4, 5, "admin");
            console.log("数组长度:" + arr.length)

        }

        function f3() {
            // JSON格式语法,相当于new Array(元素1,元素2,元素3...)
            let arr = [1, 2, 3, 4, 5];
            console.log("数组长度:" + arr.length)
            console.log(arr)
        }

        function f4() {
            let arr = [7, 3, 8, 15, 11, 1, 13, 6];
            let arr2 = [11, 2, 14, 5, 8];
            console.log("数组长度:" + arr.length)
            console.log(arr.concat(arr2))
            console.log(arr.toString());
            console.log(arr.join());
            console.log(arr.join("-"))
            console.log(arr.reverse())
            console.log(arr.sort())
            console.log(arr.sort((a, b) => a - b))
            console.log(arr.slice(2));
            console.log(arr.slice(2,5));
        }
    </script>
</head>
<body>
<button onclick="f1()">定义方式一</button>
<br>
<button onclick="f2()">定义方式二</button>
<br>
<button onclick="f3()">定义方式三</button>
<br>
<button onclick="f4()">常用方法</button>
<br>
</body>
</html>